/**
 * This is an aspect to your grid that will add in AutoFilter ability to the grid
 * You must provide the filter function for your grid. If will be called with an object
 * {
 *      fieldName:filterValue,
 *      fieldName2:filterValue2
 *      
 * }
 * to attach this plugin to your grid you mix this in to your custom grid (last)
 * and then define a filter() method
 * 
 * TODO: [Blank] and multiple filters is still a little buggy
 * 
 * @author maulin
 */
dojo.provide("medryx.widgets._GridPlugins.AutoFilter");
dojo.require("dojox.lang.aspect");
dojo.require("medryx.widgets._GridPlugins._AutoFilterTooltip");

dojo.declare("medryx.widgets._GridPlugins.AutoFilter", null, {
	
	constructor:function() {
		this._filterableValues = [];
		
	},
	
	/**
	 * return a list of values that this column can be filter by,
	 * sorted alphabetically
	 * @param {Object} column
	 */
	getFilterableValues: function(column) {
	   	var values = this._filterableValues[column.index];
		return this.cleanFilterValues(values);
	},
	
	cleanFilterValues:function(values) {
		var unique = {};
        
        //get rid of duplicates
        return dojo.filter(values, function(value) {
            if (!unique[value]) {
                unique[value] = true;
                return true;
            }
            return false;
        }).sort();
	},
	
	/**
	 * add a string value to the list of all filterable values
	 * for a column
	 * @param {Object} column
	 * @param {Object} value
	 */
	addFilterableValue:function(column, value) {
		var columnIndex = column.index;
		if (!this._filterableValues[columnIndex]) {
			this._filterableValues[columnIndex] = [];
		}
		if (typeof(value) !== 'undefined' && value !== null) {
			this._filterableValues[columnIndex].push(value);
		}
	},
	
	updateFilter:function(filter) {
        if (this.filter) {
			this.filter(filter, true);
		}
    },
	
	updateFilterValue:function(column, value) {
		var filterField = {};
		filterField[column.field] = value;
		
		if (!this.filterFields) {
            this.filterFields = {};
        }
        if (filterField) {
            dojo.mixin(this.filterFields, filterField); //makes any other filters "sticky"
        }
		
		this.updateFilter(this.filterFields);
	},
	
	getTooltip:function(column) {
		if (!this.tooltips) {
			this.tooltips = [];
		}
		if (!this.tooltips[column.index]) {
			this.tooltips[column.index] = new medryx.widgets._GridPlugins._AutoFilterTooltip({column:column.name});
			dojo.connect(this.tooltips[column.index], "onValueChanged", dojo.hitch(this, "updateFilterValue", column));
		}
		return this.tooltips[column.index];
	},
	
	/**
	 * set "autoFilter:true" in your cell properties to get this to work
	 * @param {Object} e
	 */
	showFilterDialog:function(e) {
        if (e.cell.autoFilter) {
			this.getTooltip(e.cell).show(e.pageX, e.pageY, this.getFilterableValues(e.cell));
        }
    },
	
	postCreate:function() {
		var originalGet = this.get;
		
		this.get = function(inRowIndex) {
	        var value =  originalGet.apply(this, arguments);
	        this.grid.addFilterableValue(this, value);
	        return value;
         };
		
		dojo.connect(this, "onHeaderContextMenu", this, "showFilterDialog");
		return this.inherited(arguments);
	},
	
	toString:function() {
		return "AutoFilter Plugin";
	},
	
});
